package com.imonsoft.sqm.service.basedata;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.sqm.dao.basedata.PartDAO;
import com.imonsoft.sqm.pojo.basedata.Part;
import com.imonsoft.sqm.pojo.basedata.PartExample;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.web.excel.PartExcel.PartImportDto;
import org.springframework.beans.BeanUtils;

@Service("partService")
public class PartServiceImpl implements PartService {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private PartDAO partDAO;
    @Autowired
    private SupplierService supplierService;

    @Override
    public void stopPart(List<Long> idList) {
        if(null==idList || idList.isEmpty()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        for(int i=0;i<idList.size();i++){
            Part part=partDAO.selectPartByPrimaryKey(idList.get(i));
            if (!part.getState().equals(ConstantSqm.STATUS_NORMAL)) {
                throw new SystemException(ConstantSqm.OPERATE_STOP);
            }
            part.setState(ConstantSqm.STATUS_DELETE);
            partDAO.updatePartByPrimaryKey(part);
        }
    }
    @Override
    public void startPart(List<Long> idList) {
        if (null == idList || idList.isEmpty()) {
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        for (int i = 0; i < idList.size(); i++) {
            Part part=partDAO.selectPartByPrimaryKey(idList.get(i));
            if (!part.getState().equals(ConstantSqm.STATUS_DELETE)) {
                throw new SystemException(ConstantSqm.OPERATE_START);
            }
            part.setState(ConstantSqm.STATUS_NORMAL);
            partDAO.updatePartByPrimaryKey(part);
        }
    }

    @Override
    public List<Part> findPartByCode(String code) {
        if(StringUtils.isEmpty(code)){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        PartExample example = new PartExample();
        PartExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(!StringUtils.isEmpty(code)){
            criteria.andCodeEqualTo(code);
        }
        List<Part> partList=partDAO.selectPartByExample(example);
        return partList;
    }

    @Override
    public Long insert(Part entity) {
        if(null==entity){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        if(null!=entity.getSupplierId()){
            Supplier supplier=supplierService.get(entity.getSupplierId());
            entity.setSupplierCode(supplier.getCode());
            entity.setSupplierName(supplier.getName());
        }
        entity.setState(ConstantSqm.STATUS_NORMAL);
        return partDAO.insertPart(entity);
    }

    @Override
    public int update(Part entity) {
        if(null==entity || null==entity.getId()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        Part part=partDAO.selectPartByPrimaryKey(entity.getId());
        part.setCode(entity.getCode());
        part.setName(entity.getName());
        part.setPartType(entity.getPartType());
        part.setProductType(entity.getProductType());
        part.setDrawingCode(entity.getDrawingCode());
        part.setDrawingVersionCode(entity.getDrawingVersionCode());
        part.setSupplierId(entity.getSupplierId());
        Supplier supplier=supplierService.get(entity.getSupplierId());
        part.setSupplierCode(supplier.getCode());
        part.setSupplierName(supplier.getName());

        part.setCustomerName(entity.getCustomerName());
        part.setCustomerProjectCode(entity.getCustomerProjectCode());
        part.setCarModel(entity.getCarModel());
        part.setComment(entity.getComment());
        return partDAO.updatePartByPrimaryKey(part);
    }

    @Override
    public int delete(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        return partDAO.deletePartByPrimaryKey(id);
    }

    @Override
    public Part get(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        Part part=partDAO.selectPartByPrimaryKey(id);
        return part;
    }

    @Override
    public List<Part> findList(Part entity) {
        PartExample example = new PartExample();
        PartExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getCode())){
                criteria.andCodeLike(entity.getCode()+"%");
            }
        }
        List<Part> partList=partDAO.selectPartByExample(example);
        return partList;
    }

    @Override
    public DataGrid<Part> getDataGrid(Part entity, Pager pager){
        PartExample example = new PartExample();
        example.setOrderByClause("id desc");
        PartExample.Criteria criteria = example.createCriteria();
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getCode())){
                criteria.andCodeLike(entity.getCode()+"%");
            }
            if(!StringUtils.isEmpty(entity.getName())){
                criteria.andNameLike(entity.getName()+"%");
            }
            if(null!=entity.getSupplierId()){
                criteria.andSupplierIdEqualTo(entity.getSupplierId());
            }
            if (!StringUtils.isEmpty(entity.getState())) {
                criteria.andStateEqualTo(entity.getState());
            }
        }
        return partDAO.getDataGrid(example,pager);
    }
    /**
     * ZhouWei 系统导入保存
     * @param importList
     * @return
     */
    @Override
    public void txInsertForSys(List<PartImportDto> importList) {
    	if(null==importList){
    		throw new SystemException("提交数据不能为空");
    	}
    	if(importList!=null&&importList.size()>0){
    		for (PartImportDto s:importList) {
    			Part part=new Part();
    			part.setCode(s.getCode());
    			List<Supplier> supplierList=supplierService.findSupplierByCode(s.getSupplierCode());
				if(supplierList!=null&&supplierList.size()>0){
					part.setSupplierId(supplierList.get(0).getId());
					part.setSupplierName(supplierList.get(0).getName());
				}
    			List<Part> checkHave=findList(part);
    			if(checkHave!=null&&checkHave.size()>0){
    				continue;
    			}
    			BeanUtils.copyProperties(s, part);
    			part.setState(ConstantSqm.STATUS_NORMAL);
    			partDAO.insertPart(part);
			}
    	}
    }
    @Override
    public List<Part> queryAll() {
    	PartExample example = new PartExample();
		List<Part> partList = partDAO.selectPartByExample(example);
		return partList;
    }
}
